<?php
USE_LIB('page');
USE_PAGE('index');

class PageRecherche extends Page
	{
	private $m_f_gskwds;
	private $m_modules;
	
	protected function build()
		{
		$this->m_f_gskwds = trim(GETVAL( 'f_gskwds', htmlentities(POSTVAL('f_gskwds'))));
		$this->m_modules = array( 
			'films' => array(
				'build' => 'new Films()',
				'url' => '/dvds/{VAR_F_FILMS_CATEGORIES_ID_F_TITLE_CODE},{VAR_F_FILMS_CATEGORIES_ID},{VAR_F_TITLE_CODE},{VAR_ID}',
				'filter' => 'f_status=1',
				),
			);
		}
		
		
	public function parsePHP( $file )
		{
		$moduleplus = array(
//			'pages' => array('url'=>'preferences/pages_edit,'),
//			'blockhtml' => array('url'=>'preferences/blockhtml_edit,'),
//			'blocktext' => array('url'=>'preferences/blocktext_edit,'),
			);
		$this->m_modules = array_merge( $this->m_modules, $moduleplus );
		Page::addVar( 'GENERAL_SEARCH_KEYWORDS', $this->m_f_gskwds );
		$total = 0;
		$each_modules = array();
		if( !empty($this->m_f_gskwds) )
			{
			$this->m_f_gskwds = trim(GETVAL( 'f_gskwds', htmlentities(POSTVAL('f_gskwds'))));
			foreach( $this->m_modules as $name => $params )
				{
				$url = $params['url'];
				USE_APP($name);
				$extras = ARRAYVAL($params,'build',null);
				if( isset( $extras ) )
					eval("\$obj = new $name( 'VAR', $extras );" );
				else
					$obj = new $name( 'VAR' );
				$module = $obj->getSearchParams();
				$module['url'] = $url;

				$pro =& $obj->getLocalSearchProcessor();
				if( isset( $params['filter'] ) )
					$pro->makeSearch($this->m_f_gskwds, $params['filter']);
				else
					$pro->makeSearch($this->m_f_gskwds);

				$module['MODULE_SEARCH_CARD'] = $card = $obj->getNumElements();

				if( $card<=0 ) continue;
				if( isset( $params['limit'] ) )
					{
					if( $card > $params['limit'] )
						{
						$ncard = $params['limit'];
						if( isset( $params['fitler'] ) )
							$pro->makeSearch($this->m_f_gskwds, $params['filter'] . ' LIMIT ' . $ncard );
						else
							$pro->makeSearch($this->m_f_gskwds, '1=1 LIMIT ' . $ncard );
						}
					else
						$ncard = $card;
					}
				else
					$ncard = $card;

				$results = $obj->getElementsByParams();
				$tpl = new Template;
				for( $i=0; $i<$card; $i++ )
					{
					$url = $obj->getLocalSearchUrl( $results[$i] );
					if( isset( $url ) )
						$results[$i]['url'] = $url;
					else
						{
						$tpl->setTemplateString( $module['url'] );
						$obj->setDisplayDepth(1);
						$obj->elt2html($tpl,$results[$i],'VAR_');
						$results[$i]['url'] = $tpl->getHTML();
						}
					$tpl->setTemplateString( $module['head'] );
					$obj->elt2html($tpl,$results[$i],'VAR_');
					$results[$i]['head'] = $tpl->getHTML();
					$tpl->setTemplateString( $module['body'] );
					$obj->elt2html($tpl,$results[$i],'VAR_');
					$results[$i]['body'] = $tpl->getHTML();
					}
				$module['RESULTS'] = $results;

				$total += $card;
				$module = Page::capitalize( $module );
				$each_modules[] = $module;
				}
			Page::addVar( 'GENERAL_SEARCH_RESULT', $total );
			}
		else
			Page::addVar( 'GENERAL_SEARCH_RESULT', 0 );
		Page::addVar( 'GENERAL_SEARCH_MODULES', $each_modules );
		parent::parsePHP( $file );
		}

		
	public function handleEvent( $event, $source, $params ) {parent::handleEvent( $event, $source, $params );}
	protected function _process( $processor = null, $ret = false ) { return $ret; }
	protected function getPageUrl() { return 'recherche'; }
	protected function getBreadcrumbTitle() { return 'recherche de "'.$this->m_f_gskwds.'"'; }
	protected function getPageParent() { return 'PageIndex'; }
	}
?>